<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="昵称" style="width: 200px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item m-l-2" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-checkbox v-if="!status" v-model="showReviewer" class="filter-item" style="margin-left:15px;"
        @change="tableKey = tableKey + 1">
        显示手机
      </el-checkbox>

    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row
      style="width: 100%;">
      <el-table-column label="头像" width="80">
        <template slot-scope="{row}">
          <el-image v-if="row.headimgurl" fit="cover" :src="row.headimgurl" />
          <el-image v-else fit="cover" src="https://cloud.tch1217.com/image/icon/head.png" />
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span v-if="row.nickname" class="link-type">{{ row.nickname }}</span>
          <span v-else style="color:#F56C6C">头像未授权</span>
        </template>
      </el-table-column>
      <el-table-column label="微信ID" min-width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.openid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机" min-width="110px" align="center" v-if="showReviewer">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="200px" align="center">
        <template slot-scope="{row}">
          <div @click="handleUpdate(row)">
            <span class="link-type" v-if="row.beizhu">{{ row.beizhu }}</span>
            <span class="link-type" v-else style="color:#F56C6C">未定义</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近登录" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.login_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近IP" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传权限" width="110px" align="center">
        <template slot-scope="{row}">
          <el-switch v-model="row.type" active-color="#13ce66" @change="isType(row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.status != 'deleted'" size="mini" type="success" @click="Send(row)">
            发送通知
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px"
        style="width: 400px; margin-left:50px;">
        <el-form-item label="备注" prop="beizhu">
          <el-input v-model="temp.beizhu" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData2()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { userList, updateUser } from '@/api/user'
import { WxapiSend } from '@/api/send'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },

  data() {
    return {
      status: null,
      showReviewer: null,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },

      temp: {
        id: null,
        type: null,
        beizhu: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        beizhu: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      userList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.count
        this.listLoading = false
      }).catch(err => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 推荐开关触发
    isType(row) {
      // console.log(row)
      this.updateData(row)
    },
    updateData(row) {

      updateUser({
        id: row.id,
        type: row.type
      }).then(response => {
        this.$notify({
          title: '操作成功',
          message: '是否允许上传更改成功',
          type: 'success',
          duration: 2000
        })
      }).catch(err => {
        console.log(err)
      })
    },
    Send(row) {
      WxapiSend({ touser: row.openid }).then(res => {
        this.$notify({
          title: '操作成功',
          message: res.msg,
          type: 'success',
          duration: 2000
        })
      }).catch(err => {
        console.log(err)
      })
    },
    /**
     * 编辑
     */
    // 编辑弹窗
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData2() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateUser(tempData).then(response => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          }).catch(err => {
            this.dialogFormVisible = false
          })
        }
      })
    },
  }
}
</script>
